<?php
class FieldsController extends AppController {

	var $name = 'Fields';
	var $helpers = array('Html', 'Form');

    function mylist()
    {
        $this->Field->recursive=-1;
        return $this->Field->find('all',array('order'=>'Field.name') );
    }

    function index_admin() 
    {
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout = 'ajax';
            if(!empty($this->data))
            { 
                if($this->data['Field']['name'] == '' )
                {
                    $this->set('empty',true);
                }
                else                
                if(!empty($this->data['Field']['id']))
                {
                    $count = $this->Field->find('count',array('conditions'=>array('Field.name'=>$this->data['Field']['name'],'Field.id <>'=>$this->data['Field']['id'])));
                    if( $count == 0 ) 
                    { $this->Field->save($this->data); }
                    else
                    { $this->set('wrong',true); } 
                }
                else
                {
                    $count = $this->Field->find('count',array('conditions'=>array('Field.name'=>$this->data['Field']['name'])));
                    if( $count == 0 ) 
                    { 
                        $this->Field->create(); 
                        $this->Field->save($this->data); 
                    }                
                    else
                    { $this->set('wrong',true); }
                }
            }
            $this->Field->recursive=-1;
            $fields = $this->Field->find('all',array('order'=>'Field.name'));
            $this->set('fields',$fields);
        }     
    }
    
    function delete_admin($id)
    {  
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout = 'ajax';
            $this->Field->del($id);
            $this->redirect(array('controller'=>'fields','action'=>'index_admin'));
        }        
    }

}
?>